package com.dp.temp;

import com.dp.io.ESN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;


@Component
@EnableAsync
public class hd_check_plain_UDPName {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    //下发包保任务，主要和技术任务
    @Async
    public void createHdPlan() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * FROM hd_check_plain ");
        list.forEach(item -> {
            List<String> list1 = Arrays.asList(item.get("UNAMELIST").toString().split(","));
            String a = "", b = "", c = "", d = "1";
            for (int i = 0; i < list1.size(); i++) {
                List<Map<String, Object>> list2 = jdbcTemplate.queryForList("SELECT" +
                        "  a.id a," +
                        "  c.job_name b," +
                        "  e.dep_name c" +
                        " FROM" +
                        "  sys_user a" +
                        "  LEFT JOIN sys_jobs_user b" +
                        "    ON a.id = b.user_id" +
                        "  LEFT JOIN sys_jobs c" +
                        "    ON b.job_id = c.job_id" +
                        "  LEFT JOIN sys_department_jobs d" +
                        "    ON c.job_id = d.job_id" +
                        "  LEFT JOIN sys_department e" +
                        "    ON d.dep_id = e.dep_id" +
                        " WHERE a.nickname = '" + list1.get(i) + "' limit 1");
                if (list2.size() > 0) {
                    if (i == 0) {
                        a += list2.get(0).get("a").toString();
                        b += list2.get(0).get("b").toString();
                        c += list2.get(0).get("c").toString();
                        d = "0";
                    } else {
                        a += "," + list2.get(0).get("a").toString();
                        b += "," + list2.get(0).get("b").toString();
                        c += "," + list2.get(0).get("c").toString();
                    }
                } else {
                    if (i == 0) {
                        a += "无此人";
                        b += "无此人";
                        c += "无此人";
                    } else {
                        a += "," + "无此人";
                        b += "," + "无此人";
                        c += "," + "无此人";
                    }
                }
            }
            jdbcTemplate.execute("UPDATE hd_check_plain SET DNAME = '" + c + "',PNAME = '" + b + "',UID = '" + a + "',IFSTAR = '" + d + "' WHERE ID = '" + item.get("ID") + "'");
        });

    }



}
